package com.tgy.leetcode;

import java.util.HashMap;

/**
 * @Author: tgy
 * @Date: 12/26/20 3:03 PM
 */
public class _138_复制带随机指针的链表 {

    public Node copyRandomList(Node head) {

        HashMap<Node, Node> map = new HashMap<>();

        Node node = head;
        while (node != null) {

            Node newNode = new Node(node.val);
            map.put(node,newNode);
            node = node.next;
        }


        node = head;

        while (node != null) {

            Node tmpNode = map.get(node);
            tmpNode.next = map.get(node.next);
            tmpNode.random = map.get(node.random);
            node = node.next;
        }

        return map.get(head);
    }
}
